package com.zqc.email.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api")
public class EmailController {
    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String emailFrom;

    @GetMapping("/email/{email}/{code}")
    public Boolean sendEmail(
            @PathVariable(value = "email") String email,
            @PathVariable(value = "code") String code) {
        System.out.println("=======>>>>>访问到EmailController...." + code + email);
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 发送方
        simpleMailMessage.setFrom(emailFrom);
        // 接收方
        simpleMailMessage.setTo(email);
        // 主题
        simpleMailMessage.setSubject("注册验证码");
        // 内容
        simpleMailMessage.setText("您的注册验证码：" + code);

        javaMailSender.send(simpleMailMessage);

        return true;
    }
}
